/*固定导航*/
$(function(){
	$("#checkbox_fixednavbar").attr('checked',true);
	$("#checkbox_fixedsidebar").attr('checked',true);
	$("#checkbox_fixedbreadcrumbs").attr('checked',true);
	$("#checkbox_fixedheader").attr('checked',true);
	
	if($("#checkbox_fixednavbar").is(":checked")){
		$('.navbar').addClass('navbar-fixed-top');
		$('.page-content').css('margin-top',$('.navbar-inner').height());
	}
	
	if($("#checkbox_fixedsidebar").is(":checked")){
		$('.navbar').addClass('navbar-fixed-top');
		$('.page-sidebar').addClass('sidebar-fixed');
		$('.page-content').css('margin-top',$('.navbar-inner').height());
	}
	
	if($("#checkbox_fixedbreadcrumbs").is(":checked")){
		$('.navbar').addClass('navbar-fixed-top');
		$('.page-sidebar').addClass('sidebar-fixed');
		$('.page-breadcrumbs').addClass('breadcrumbs-fixed');
		$('.page-content').css('margin-top',$('.navbar-inner').height()+$('.page-breadcrumbs').height());
	}
	
	if($("#checkbox_fixedheader").is(":checked")){
		$('.navbar').addClass('navbar-fixed-top');
		$('.page-sidebar').addClass('sidebar-fixed');
		$('.page-breadcrumbs').addClass('breadcrumbs-fixed');
		$('.page-header').addClass('page-header-fixed');
		$('.page-content').css('margin-top',$('.navbar-inner').height()+$('.page-breadcrumbs').height()+$('.page-header').height());
	}
	$('.setting-container input').on('click',function(){
		if($(this).is("#checkbox_fixednavbar")){
			if($(this).is(':checked')){
				$('.page-content').css('margin-top',$('.navbar-inner').height());
			}else{
				$('.page-content').css('margin-top',0);
			} 
		}else if($(this).is("#checkbox_fixedsidebar")){
			if($(this).is(':checked')){
				$('.page-content').css('margin-top',$('.navbar-inner').height());
			}else{
				$('.page-content').css('margin-top',$('.navbar-inner').height());
			} 
		}else if($(this).is("#checkbox_fixedbreadcrumbs")){
			if($(this).is(':checked')){
				$('.page-content').css('margin-top',$('.navbar-inner').height()+$('.page-breadcrumbs').height());
			}else{
				$('.page-content').css('margin-top',$('.navbar-inner').height());
			}  
		}else if($(this).is("#checkbox_fixedheader")){
			if($(this).is(':checked')){
				$('.page-content').css('margin-top',$('.navbar-inner').height()+$('.page-breadcrumbs').height()+$('.page-header').height());
			}else{
				$('.page-content').css('margin-top',$('.navbar-inner').height()+$('.page-breadcrumbs').height());
			}  
		}
		
	})
	//修改头像
	$('#saveHeadImage').on('click',function(){
		var formData = new FormData($("#avatarForm")[0]);
		$.ajax({
			url : ctx + "/user/changeHeadImage",
			type : 'POST',
			data : formData,
			async : false,
			cache : false,
			contentType : false,
			processData : false,
			success : function(data) {
				if (data.msg) {
					toastr.error(data.msg);
				} else {
					$('#avatar-modal').modal('hide');
					$('#headImage').attr('src', data.url);
					$('#bigHeadImage').attr('src', data.url);
					toastr.success('修改成功');
					
				}
			}
		});
		return false;
	});
	
	 //表单验证
    $('#changePassword').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            oldPassword: {
                validators: {
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度最小6位最长30位'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码只能有数字、字母、下划线组成'
                    }
                }
            },
            newPassword: {
                validators: {
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度最小6位最长30位'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码只能有数字、字母、下划线组成'
                    }
                }
            },
            affirmPassword: {
                validators: {
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度最小6位最长30位'
                    },
                    identical: {
                        field: 'newPassword',
                        message: '两次密码输入不一致'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码只能有数字、字母、下划线组成'
                    }
                }
            },
        },
            submitHandler: function (validator, form, submitButton) {
	            $.ajax({
	            	url: ctx + "/user/changePwd",
	                type: 'POST',
	                data:  $('#changePassword').serialize(),
	                dataType: 'json',
	                success: function (data) {
	                    if (data.msg){
	                    	toastr.error(data.msg);
	                    }else{
	                    	$('.changePassword').modal('hide');
	                    	toastr.success('修改成功');
	                    	location.href=ctx+'/login/logout'
	                    }
	                },
	                error: function (response) {
	                	toastr.error("系统错误");
	                }
	            });
	            return false;
	        }
    });
})